<?php
class Home_m extends Model{
      function Home_m(){
          parent::Model();
          $this->load->database();
      }
      function get_contenu_by_ID($id){
        $query = $this->db->get_where('contenu', array('id'=>$id));  
        return $query;  
      }
      function get_image($page){
        $query=$this->db->get_where('image',array('page'=>$page));
        //$this->db->order_by('id','DESC');
        return $query->result_array();
     }
     function get_image_by_name($name){
        $query=$this->db->get_where('image',array('name'=>$name));
        //$this->db->order_by('id','DESC');
        return $query;         
     }
     function get_contenu_by_name($titre){
        $query = $this->db->get_where('contenu', array('titre'=>$titre));  
        $row=$query->row_array();
        $description=$row['description'];
        return $description;  
      }
     function contact($data){
        return $this->db->insert('contact',$data); 
     }
   function get_sports(){
        $query=$this->db->get('sports');
        return $query->result_array();
   }
   function get_secteur(){
        $query=$this->db->get('secteur');
        return $query->result_array();
   }
   function get_region(){
        $query=$this->db->get('region');
        return $query->result_array();
   }
   function get_recherche(){
        $query=$this->db->get('recherche');
        return $query->result_array();
   }
    function get_sports_array($string){
        $this->db->select();
        $this->db->from('sports');
        $this->db->where('id in ('.$string.')');
        $query =$this->db->get();
        return $query->result_array();
   }
   function get_recherche_array($string){
        $this->db->select();
        $this->db->from('recherche');
        $this->db->where('id in ('.$string.')');
        $query =$this->db->get();
        return $query->result_array();
   }
   function recherche_adherent($sport,$secteur,$region,$per_page,$offset){
        
       $query1=$this->db->get('inscription');
            $test=array();
            foreach ($query1->result_array() as $row):
            $i=0;
            $array=explode(",",$row['sport']);
                foreach ($array as $aRow):
                    if ($aRow==$sport){$i=$i+1;}
                endforeach;
            if ($i==1){array_push($test,$row['id']);}
            
       endforeach; 
       $this->db->select('inscription.*,sports.nom as sportnom,recherche.nom as recherchenom,secteur.nom as secteurnom');
        $this->db->from('inscription');
        $this->db->join('sports','inscription.sport=sports.id','left');
        $this->db->join('recherche','inscription.recherche=recherche.id','left');
        $this->db->join('secteur','inscription.secteur=secteur.id','left');
       if($sport>0){
           $idsport="";
           if(count($test)>0){
           foreach ($test as $aRow1):
                $idsport.=" , ".$aRow1." ";
           endforeach;
           $text= substr($idsport, 3);
           $this->db->where('inscription.id in ('.$text.')');
           }
           else $this->db->where('sport',$sport);
       }   
       if($secteur>0){
       $this->db->where('secteur',$secteur);
       }
       if($region>0){
       $this->db->like('postale',$region,'after');
       }
       $this->db->where('ad',1);
       $this->db->limit($per_page,$offset);

       $query=$this->db->get();
       return $query;
       
   }
      function recherche_adherent1($sport,$secteur,$region){
                 $query1=$this->db->get('inscription');
            $test=array();
            foreach ($query1->result_array() as $row):
            $i=0;
            $array=explode(",",$row['sport']);
                foreach ($array as $aRow):
                    if ($aRow==$sport){$i=$i+1;}
                endforeach;
            if ($i==1){array_push($test,$row['id']);}
       endforeach;
        $this->db->select('inscription.*,sports.nom as sportnom,recherche.nom as recherchenom,secteur.nom as secteurnom');
        $this->db->from('inscription');
        $this->db->join('sports','inscription.sport=sports.id','left');
        $this->db->join('recherche','inscription.recherche=recherche.id','left');
        $this->db->join('secteur','inscription.secteur=secteur.id','left');
       if($sport>0){
           $idsport="";
           if(count($test)>0){
           foreach ($test as $aRow1):
                $idsport.=" , ".$aRow1." ";
           endforeach;
           $text= substr($idsport, 3);
           $this->db->where('inscription.id in ('.$text.')');}
           else $this->db->where('sport',$sport); 
       }
       if($secteur>0){
       $this->db->where('secteur',$secteur);
       }
       if($region>0){
       $this->db->like('postale',$region,'after');
       }
       $this->db->where('ad',1);
       $query=$this->db->get();
       return $query;
       
   }
   function get_adherent_by_ID($id){
        $this->db->select('inscription.*,sports.nom as sportnom,recherche.nom as recherchenom,secteur.nom as secteurnom');
        $this->db->from('inscription');
        $this->db->join('sports','inscription.sport=sports.id','left');
        $this->db->join('recherche','inscription.recherche=recherche.id','left');
        $this->db->join('secteur','inscription.secteur=secteur.id','left');
        $this->db->where('inscription.id',$id);
        
        $query = $this->db->get();  
        return $query;  
      }
   function check_connexion($compte){
        $email=$compte['email'];
        $pass=$compte['pass'];
        $query = $this->db->get_where('inscription', array('email'=>$email,'pass'=>$pass));
        return $query;
      }

    function get_mail($id){
           $this->db->select();
           $this->db->from('mail_auto');
            $this->db->where("id",$id);
           $query=$this->db->get();
           return $query->row_array();
     }
        function update_client($data, $id){
       return $this->db->update('inscription',$data,array('id'=>$id));
    }
    function check_email($email){
       $query = $this->db->get_where('inscription', array('email'=>$email));
       return $query;
    }

   function recherche_contenu($keyword,$per_page,$offset){
       $this->db->select();
       $this->db->from('contenu');
       $this->db->like('titre',$keyword);
       $this->db->or_like('description',$keyword);
       $this->db->limit($per_page,$offset);
       $query=$this->db->get();
       return $query;         
   }
   function recherche_contenu1($keyword){
       $this->db->select();
       $this->db->from('contenu');
       $this->db->like('titre',$keyword);
       $this->db->or_like('description',$keyword);
       $query=$this->db->get();
       return $query;       
   }


}    
?>

